<?php
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}
include_once 'public/DB_Oracle.php';
$DBsql = new DB_Oracle ();

//根据选择的单位id查询该单位下的警员、警车
if(isset($_POST['act']) && $_POST['act'] == 'select' && $_POST['selectedID'] !== ''){
	$id = $_POST['selectedID'];
	//查询警员
	$sql_for_per = "select t.personnel_index,t.personnel_name,t.personnel_sn from dep_personnel t where t.department_index in (".$id.")";
	$row_for_per = $DBsql->fetch_all_array($sql_for_per);
	$str_for_per = '';
	foreach($row_for_per as $per){
		$str_for_per .= '<option value="'.$per['PERSONNEL_INDEX'].'">'.$per['PERSONNEL_SN'].' '.$per['PERSONNEL_NAME'].'</option>';
	}
	//查询警车
	$sql_for_veh  = "select v.vehicle_index,v.license_plate,vt.vehicle_type_name from dep_vehicle v,dep_vehicle_type vt where v.vehicle_type_index = vt.vehicle_type_index and v.department_index in (".$id.")";
	$row_for_veh = $DBsql->fetch_all_array($sql_for_veh);
	$str_for_veh = '';
	foreach($row_for_veh as $veh){
		$str_for_veh .= '<option value="'.$veh['VEHICLE_INDEX'].'">'.$veh['LICENSE_PLATE'].' '.$veh['VEHICLE_TYPE_NAME'].'</option>';
	}
	//输出数据
	echo $str_for_per.'|'.$str_for_veh;
}

//添加案件
if(isset($_GET['act']) && $_GET['act'] == 'add'){
	$name 		= $_POST['name']; //案件名称
	$department = implode(',', $_POST['department']); //单位id
	$vehicleStr  = isset($_POST['vehicle'])?implode(',', $_POST['vehicle']):''; //车辆id
	$personalStr = isset($_POST['personal'])?implode(',', $_POST['personal']):''; //警员id
	$member		= $vehicleStr.'#'.$personalStr; //参与人员及车辆
	$start		= $_POST['StartTime'];	//开始时间
	$end		= $_POST['EndTime'];	//结束时间
	$sql		= "INSERT INTO DEP_MISSION (MISSION_NAME, MISSION_MEMBER, MISSION_START, MISSION_END, DEPARTMENT_INDEX) VALUES ('".$name."', '".$member."', to_date('".$start."','yyyy-mm-dd hh24-mi-ss'), to_date('".$end."','yyyy-mm-dd hh24-mi-ss'), '".$department."')";
	if($DBsql->query($sql)){
		echo '{"info":"案件添加成功！","status":"y"}';
	}else{
		echo '{"info":"案件添加失败！","status":"n"}';
	}
}

//删除案件
if(isset($_POST['act']) && $_POST['act'] == 'del' && $_POST['id'] !== ''){
	$id  = $_POST['id'];
	$sql = 'delete from dep_mission m where m.mission_index in('.$id.')';
	if($DBsql->query($sql)){
		echo 'y';
	}else{
		echo 'n';
	}
}

//编辑案件
if(isset($_POST['act']) && $_POST['act'] == 'edit' && $_POST['id'] !== ''){
	$id  = $_POST['id'];
	$sql = 'select t.mission_index,t.mission_name,t.mission_member,to_char(t.mission_start, \'yyyy-mm-dd hh24:mi:ss\') STARTTIME,to_char(t.mission_end, \'yyyy-mm-dd hh24:mi:ss\') ENDTIME,t.department_index from dep_mission t where t.mission_index = '.$id;
	$arr = $DBsql->fetch_array($sql);
	$result = '';
	//单位ID
	$departmentID = $arr['DEPARTMENT_INDEX'];
	//警员ID 警车ID
	$IDarr = explode("#", $arr['MISSION_MEMBER']);
	$personnelID = $IDarr[1];
	$vehicleID = $IDarr[0];
	//查询警员
	$sql_for_per = "select t.personnel_index,t.personnel_name,t.personnel_sn from dep_personnel t where t.department_index in (".$departmentID.")";
	$row_for_per = $DBsql->fetch_all_array($sql_for_per);
	$str_for_per = '';
	foreach($row_for_per as $per){
		if(in_array($per['PERSONNEL_INDEX'], explode(",", $personnelID))){ //判断是否为选中状态
			$str_for_per .= '<option selected="selected" value="'.$per['PERSONNEL_INDEX'].'">'.$per['PERSONNEL_SN'].' '.$per['PERSONNEL_NAME'].'</option>';
		}else{
			$str_for_per .= '<option value="'.$per['PERSONNEL_INDEX'].'">'.$per['PERSONNEL_SN'].' '.$per['PERSONNEL_NAME'].'</option>';
		}
	}
	//查询警车
	$sql_for_veh  = "select v.vehicle_index,v.license_plate,vt.vehicle_type_name from dep_vehicle v,dep_vehicle_type vt where v.vehicle_type_index = vt.vehicle_type_index and v.department_index in (".$departmentID.")";
	$row_for_veh = $DBsql->fetch_all_array($sql_for_veh);
	$str_for_veh = '';
	foreach($row_for_veh as $veh){
		if(in_array($veh['VEHICLE_INDEX'], explode(",", $vehicleID))){ //判断是否为选中状态
			$str_for_veh .= '<option selected="selected" value="'.$veh['VEHICLE_INDEX'].'">'.$veh['LICENSE_PLATE'].' '.$veh['VEHICLE_TYPE_NAME'].'</option>';
		}else{
			$str_for_veh .= '<option value="'.$veh['VEHICLE_INDEX'].'">'.$veh['LICENSE_PLATE'].' '.$veh['VEHICLE_TYPE_NAME'].'</option>';
		}
	}
	//输出数据
	echo $arr['MISSION_INDEX'].'|'.$arr['MISSION_NAME'].'|'.$str_for_per.'|'.$str_for_veh.'|'.$arr['STARTTIME'].'|'.$arr['ENDTIME'].'|'.$arr['DEPARTMENT_INDEX'];
}

//更新案件
if(isset($_GET['act']) && $_GET['act'] == 'update'){
	$name 		 = $_POST['ename']; //案件名称
	$department  = implode(',', $_POST['edepartment']); //单位ID
	$vehicleStr  = isset($_POST['evehicle'])?implode(',', $_POST['evehicle']):''; //车辆id
	$personalStr = isset($_POST['epersonal'])?implode(',', $_POST['epersonal']):''; //警员id
	$member		 = $vehicleStr.'#'.$personalStr; //参与人员及车辆
	$start		 = $_POST['eStartTime'];	//开始时间
	$end		 = $_POST['eEndTime'];		//结束时间
	$id			 = $_POST['editID'];		//案件id

	$sql		 = "UPDATE dep_mission t SET t.mission_name = '".$name."',t.mission_member = '".$member."',t.mission_start = to_date('".$start."','yyyy-mm-dd hh24-mi-ss'),t.mission_end = to_date('".$end."','yyyy-mm-dd hh24-mi-ss'),t.department_index = '".$department."' WHERE t.mission_index = '".$id."'";
  
	if($DBsql->query($sql)){
		echo '{"info":"案件修改成功！","status":"y"}';
	}else{
		echo '{"info":"案件修改失败！","status":"n"}';
	}
}


?>